@(active: String,
  issue: gitbucket.core.model.Issue,
  pullreq: gitbucket.core.model.PullRequest,
  commits: Seq[gitbucket.core.util.JGitUtil.CommitInfo],
  comments: Seq[gitbucket.core.model.Comment],
  changedFileSize: Int,
  isManageable: Boolean,
  repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
  flash: Map[String, String] = Map.empty)(body: => Html)(implicit context: gitbucket.core.controller.Context)
@import gitbucket.core.view.helpers
@import gitbucket.core.model.Comment
@import gitbucket.core.model.CommitComments
@import gitbucket.core.model.IssueComment
@gitbucket.core.html.main(s"${issue.title} - Pull request #${issue.issueId} - ${repository.owner}/${repository.name}", Some(repository)) {
  @gitbucket.core.html.menu("pulls", repository) {
      <div>
        <div class="show-title pull-right">
          @if(isManageable || context.loginAccount.map(_.userName == issue.openedUserName).getOrElse(false)) {
            <a class="btn btn-default" href="#" id="edit">Edit</a>
          }
          @if(context.loginAccount.isDefined) {
            <a class="btn btn-success" href="@helpers.url(repository)/compare">New pull request</a>
          }
        </div>
        <div class="edit-title pull-right" style="display: none;">
          <a class="btn btn-success" href="#" id="update">Save</a>  <a class="btn btn-default" href="#" id="cancel">
          Cancel</a>
        </div>
        <h1 class="body-title">
          <span class="show-title">
            <span id="show-title">@issue.title</span>
            <span class="muted">#@issue.issueId</span>
          </span>
          <span class="edit-title" style="display: none;">
            <span id="error-edit-title" class="error"></span>
            <input type="text" class="form-control" style="width: 700px;" id="edit-title" value="@issue.title"/>
          </span>
        </h1>
      </div>
      <div style="margin-bottom: 15px">
      @if(issue.closed) {
        @comments.flatMap @{
          case comment: IssueComment => Some(comment)
          case _ => None
        }.find(_.action == "merge").map { comment =>
        <span class="label label-info issue-status">Merged</span>
        <span class="muted">
          @helpers.user(comment.commentedUserName, styleClass = "username strong")
          merged @commits.size @helpers.plural(commits.size, "commit")
          into <code><a href="@helpers.basePRBranchUrl(pullreq)">@pullreq.userName:@pullreq.branch</a></code>
          from <code><a href="@helpers.requestPRBranchUrl(pullreq)">@pullreq.requestUserName:@pullreq.requestBranch</a></code>
          @gitbucket.core.helper.html.datetimeago(comment.registeredDate)
        </span>
        }.getOrElse {
          <span class="label label-important issue-status">Closed</span>
          <span class="muted">
            @helpers.user(issue.openedUserName, styleClass = "username strong")
            wants to merge @commits.size @helpers.plural(commits.size, "commit")
            into <code><a href="@helpers.basePRBranchUrl(pullreq)">@pullreq.userName:@pullreq.branch</a></code>
            from <code><a href="@helpers.requestPRBranchUrl(pullreq)">@pullreq.requestUserName:@pullreq.requestBranch</a></code>
          </span>
        }
      } else {
        <span class="label label-success issue-status">Open</span>
        <span class="muted">
          @helpers.user(issue.openedUserName, styleClass = "username strong")
          wants to merge @commits.size @helpers.plural(commits.size, "commit")
          into <code><a href="@helpers.basePRBranchUrl(pullreq)">@pullreq.userName:@pullreq.branch</a></code>
          from <code><a href="@helpers.requestPRBranchUrl(pullreq)">@pullreq.requestUserName:@pullreq.requestBranch</a></code>
        </span>
      }
      </div>
      <ul class="nav nav-tabs fill-width" id="pullreq-tab">
        <li @if(active=="conversation"){class="active"}><a href="@helpers.url(repository)/pull/@issue.issueId">Conversation <span class="badge">@countConversation(comments)</span></a></li>
        <li @if(active=="commits"){class="active"}><a href="@helpers.url(repository)/pull/@issue.issueId/commits">Commits <span class="badge">@commits.size</span></a></li>
        <li @if(active=="files"){class="active"}><a href="@helpers.url(repository)/pull/@issue.issueId/files">Files Changed <span class="badge">@changedFileSize</span></a></li>
      </ul>
      <div class="tab-content fill-width" style="padding-top: 20px;">
        @flash.get("error").map { error =>
          <div class="alert alert-error">@error</div>
        }
        @flash.get("info").map { info =>
          <div class="alert alert-info">@info</div>
        }
        @body
      </div>
  }
}
<script>
  $(function(){
   $('#edit').click(function(){
      $('.edit-title').show();
      $('.show-title').hide();
      $('#edit-title').focus();
      return false;
    });

    $('#update').click(function(){
      $(this).attr('disabled', 'disabled');
      $.ajax({
        url: '@helpers.url(repository)/issues/edit_title/@issue.issueId',
        type: 'POST',
        data: {
          title   : $('#edit-title').val()
        }
      }).done(function(data){
        $('#show-title').empty().text(data.title);
        $('#cancel').click();
        $(this).removeAttr('disabled');
      }).fail(function(req){
        $(this).removeAttr('disabled');
        $('#error-edit-title').text($.parseJSON(req.responseText).title);
      });
      return false;
    });

    $('#cancel').click(function(){
      $('.edit-title').hide();
      $('.show-title').show();
      return false;
    });
  });
</script>

@countConversation(comments: Seq[Comment]) = @{
  comments.collect {
    case c: CommitComments => c.comments.size
    case c: IssueComment if c.action.endsWith("comment") => 1
  }.sum
}
